package com.ghc.androidexample1;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.Menu;
import android.widget.TextView;

public class NFCActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_nfc);

		handleViewIntent();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.nfc, menu);
		return true;
	}

	private void handleViewIntent() {
		// Get the Intent action
		Intent intent = getIntent();
		String action = intent.getAction();
		/*
		 * For ACTION_VIEW, the Activity is being asked to display data. Get the
		 * URI.
		 */
		if (TextUtils.equals(action, Intent.ACTION_VIEW)) {
			// Get the URI from the Intent
			Uri uri = intent.getData();
			try {
				// Get File info
//				Cursor cursor = getContentResolver().query(uri, null, null,
//						null, null);
//				int nameIndex = cursor
//						.getColumnIndex(OpenableColumns.DISPLAY_NAME);
//				cursor.moveToFirst();
//				String name = cursor.getString(nameIndex);
//				cursor.close();

				/*
	             * Test for the type of URI, by getting its scheme value
	             */
				File file = null;
				String name = null;
	            if (TextUtils.equals(uri.getScheme(), "file")) {
	                file = handleFileUri(uri);
	            } else if (TextUtils.equals(
	                    uri.getScheme(), "content")) {
	                file = handleContentUri(uri);
	            }
				if(file != null) {
					name = file.getName();
				}
				// Get File data
				ParcelFileDescriptor mInputPFD = getContentResolver()
						.openFileDescriptor(uri, "r");
				// Get a regular file descriptor for the file
				FileDescriptor fd = mInputPFD.getFileDescriptor();
				FileInputStream inputStream = new FileInputStream(fd);
				int length = 0;
				int total = 0;
				byte[] buffer = new byte[1024];
				while ((length = inputStream.read(buffer)) != -1) {
					total += length;
				}
				inputStream.close();
				// Show info
				TextView tvFile = (TextView) findViewById(R.id.tvFileName);
				tvFile.setText(String.format("File: %s Size: %s", name, total));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public File handleFileUri(Uri beamUri) {
		// Get the path part of the URI
		String fileName = beamUri.getPath();
		// Create a File object for this filename
		File copiedFile = new File(fileName);
		// Get a string containing the file's parent directory
		return copiedFile;
	}

	public File handleContentUri(Uri beamUri) {
		// Position of the filename in the query Cursor
		int filenameIndex;
		// File object for the filename
		File copiedFile;
		// The filename stored in MediaStore
		String fileName;
		// Test the authority of the URI
		if (!TextUtils.equals(beamUri.getAuthority(), MediaStore.AUTHORITY)) {
			/*
			 * Handle content URIs for other content providers
			 */
			// For a MediaStore content URI
		} else {
			// Get the column that contains the file name
			String[] projection = { MediaStore.MediaColumns.DATA };
			Cursor pathCursor = getContentResolver().query(beamUri, projection,
					null, null, null);
			// Check for a valid cursor
			if (pathCursor != null && pathCursor.moveToFirst()) {
				// Get the column index in the Cursor
				filenameIndex = pathCursor
						.getColumnIndex(MediaStore.MediaColumns.DATA);
				// Get the full file name including path
				fileName = pathCursor.getString(filenameIndex);
				// Create a File object for the filename
				copiedFile = new File(fileName);
				// Return the parent directory of the file
				return copiedFile;
			} else {
				// The query didn't work; return null
				return null;
			}
		}
		return null;
	}
}
